﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
using System.Data;


namespace TranTekBE.KPI_NhanSu
{
    public partial class Phat_Nhan_Vien : PagePopUp
    {
        string get_user_name = null;
        string get_id_user = null;
        string get_full_name = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Load JS
            ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "LoadJS", "LoadJS();", true);

            get_id_user = FunctionsUtils.GetValueStringFromQueryString("IDUserNamePunish", "0");

            string sql_get_user_danh_gia = "SELECT * FROM tbl_UserKPI Where ID_USER = '" + get_id_user + "'";
            DataSet sql_popup_danh_gia_user = FunctionsUtils.Database2.ExecuteQuery(sql_get_user_danh_gia);
            if (sql_popup_danh_gia_user != null)
            {
                //
                get_user_name = sql_popup_danh_gia_user.Tables[0].Rows[0]["NAME_USER"].ToString();
                get_full_name = sql_popup_danh_gia_user.Tables[0].Rows[0]["HO_USER"].ToString()
                    + "" + sql_popup_danh_gia_user.Tables[0].Rows[0]["TEN_USER"].ToString();
            }

            if (!IsPostBack)
            {
                GetDropList_LyDoPhat(droplist_LyDo);
                txtNgayPhat.Text = DateTime.Now.ToShortDateString();
                txtUserName.Text = get_full_name;
            }
        }

        protected void btnAdd_XuPhat_Click(object sender, EventArgs e)
        {
            if (txtNgayPhat.Text != "" && droplist_LyDo.SelectedValue.ToString() != "")
            {
                string nguoi_quyet_dinh_xu_phat = UserInfo.UserName;
                string ngay_phat = txtNgayPhat.Text.ToString();
                string ly_do_phat = droplist_LyDo.SelectedItem.Text;
                //
                string id_lydo = droplist_LyDo.SelectedValue.ToString();
                string sql_select_tien_tru = "SELECT * FROM tbl_DropListLyDoPhat WHERE ID_LYDO =" + id_lydo;
                DataSet select_tien_tru = FunctionsUtils.Database2.ExecuteQuery(sql_select_tien_tru);
                string get_select_tien_tru = select_tien_tru.Tables[0].Rows[0]["TIEN_TRU"].ToString();
                //
                int tien_phat = FunctionsUtils.ConvertToInt(get_select_tien_tru, 0);
                int diem_tru = tien_phat / 1000;
                
                try
                {
                    // Insert thông tin lịch làm việc người dùng 
                    string sql_add_user_phat = @"
                                    INSERT INTO tbl_XuPhat
                                    (ID_USER,NAME_USER,NGAY_PHAT,LI_DO,TIEN_PHAT,DIEM_TRU,NGUOI_PHAT)
                                    VALUES
                                    (
                                            '" + get_id_user + @"',
                                            '" + get_user_name + "','"
                                            + ngay_phat + "',N'"
                                            + ly_do_phat + "',"
                                            + tien_phat + ","
                                            + diem_tru + ",'"
                                            + nguoi_quyet_dinh_xu_phat
                                            + "')";

                    FunctionsUtils.Database2.ExecuteNonQuery(sql_add_user_phat);
                    Page page = (HttpContext.Current.Handler as Page);
                    if (page != null)
                    {
                        //page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "key", "alert('Bạn đã đánh giá thành công!')", true);
                        Response.Write("<script>alert('Bạn đã xử lý thành công!');</script>");
                        ClosePopup(true);
                    }
                }
                catch (Exception ex)
                {
                    ltAddErr.Text = ex.Message;
                }
            }
            else
            {
                ltAddErr.Text = "Nhập thiếu thông tin! Vui lòng kiểm tra lại";
                ltAddErr.ForeColor = System.Drawing.Color.Red;
            }
        }
        public static void GetDropList_LyDoPhat(DropDownList ddl)
        {
            DataSet ds_lydophat = FunctionsUtils.Database2.ExecuteQuery("SELECT * FROM tbl_DropListLyDoPhat");
            FunctionsUtils.BindDataForControlList(ddl, ds_lydophat, "ID_LYDO", "NAME_LYDO", "", "Chọn Lý Do");
        }
    }
}